import { ConfigEnv, defineConfig } from "vite"
import { buildPlugin } from "vite-plugin-build"

export default defineConfig((config: ConfigEnv) => {
  const isDev = process.argv.find((item) => item === "-w" || item === "--watch") !== undefined
  const watch = isDev ? {} : null
  const target = "es2015"
  return {
    resolve: {
      alias: {
        "@": "/src",
      },
    },
    plugins: [
      buildPlugin({
        fileBuild: {
          emitDeclaration: true,
        },
        libBuild: {
          buildOptions: [
            {
              watch,
              target,
              rollupOptions: {
                external: ["dayjs"],
                output: { globals: { dayjs: "Dayjs" } },
              },
              lib: {
                entry: "src/index.ts",
                name: "index",
                formats: ["cjs", "es"],
                fileName: (format) => `index.${format}.js`,
              },
              sourcemap: isDev,
            },
            {
              watch,
              target,
              outDir: "dist",
              lib: {
                entry: "src/examples/index.ts",
                name: "examples",
                formats: ["cjs"],
                fileName: (format) => `examples.${format}.js`,
              },
              sourcemap: isDev,
            },
          ],
        },
      }),
    ],
  }
})
